/*
 * Copyright (c) 2018-2999 九五云信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.groupbuy.platform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.dto.ProdAndSkuListsDto;
import com.yami.shop.bean.enums.OfflineHandleEventType;
import com.yami.shop.bean.model.OfflineHandleEvent;
import com.yami.shop.bean.param.OfflineHandleEventAuditParam;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.groupbuy.comment.dto.GroupActivityDto;
import com.yami.shop.groupbuy.comment.dto.GroupProdAndSkuListDto;
import com.yami.shop.groupbuy.comment.dto.GroupProdDto;
import com.yami.shop.groupbuy.comment.enums.ActivityStatusEnum;
import com.yami.shop.groupbuy.comment.model.GroupActivity;
import com.yami.shop.groupbuy.comment.model.GroupProd;
import com.yami.shop.groupbuy.comment.service.GroupActivityService;
import com.yami.shop.security.platform.util.SecurityUtils;
import com.yami.shop.service.OfflineHandleEventService;
import com.yami.shop.service.ProductService;
import lombok.RequiredArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 平台 --拼团活动
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/platform/group/activity")
public class GroupActivityController {

    private final GroupActivityService groupActivityService;
    private final OfflineHandleEventService offlineHandleEventService;

    /**
     * 分页查询
     *
     * @param page             分页对象
     * @param groupActivityDto 拼团活动表
     * @return 分页数据
     */
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('group:activity:page')")
    public ResponseEntity<IPage<GroupActivityDto>> getGroupActivityPage(PageParam<GroupActivityDto> page, GroupActivityDto groupActivityDto) {
        return ResponseEntity.ok(groupActivityService.getGroupActivityPage(page, groupActivityDto));
    }

    /**
     * 通过id查询拼团活动表
     *
     * @param groupActivityId id
     * @return 单个数据
     */
    @GetMapping("/info/{groupActivityId}")
    @PreAuthorize("@pms.hasPermission('group:activity:info')")
    public ResponseEntity<GroupActivity> getById(@PathVariable("groupActivityId") Long groupActivityId) {
        return ResponseEntity.ok(groupActivityService.getById(groupActivityId));
    }

    /**
     * 通过id删除拼团活动表
     *
     * @param groupActivityId id
     * @return 通过id删除拼团活动表
     */
    @SysLog("删除拼团活动表")
    @DeleteMapping("/{groupActivityId}")
    @PreAuthorize("@pms.hasPermission('group:activity:delete')")
    public ResponseEntity<Boolean> removeById(@PathVariable Long groupActivityId) {
        // 查看活动是否为失效状态或者活动结束
        GroupActivity groupActivity = groupActivityService.getById(groupActivityId);
        if (Objects.isNull(groupActivity)) {
            // 未找到此活动，请稍后重试
            throw new YamiShopBindException("yami.activity.cannot.find");
        }
        // 如果活动状态不在 【未开始】、【已结束】、【已失效】的状态下无法进行删除操作
        if (!(Objects.equals(groupActivity.getActivityStatus(), ActivityStatusEnum.EXPIRED.value()) ||
                Objects.equals(groupActivity.getActivityStatus(), ActivityStatusEnum.FINISHED.value()) ||
                Objects.equals(groupActivity.getActivityStatus(), ActivityStatusEnum.NOT_STARTED.value()))) {
            // 该活动状态下不能进行删除
            throw new YamiShopBindException("yami.activity.cannot.delete");
        }
        groupActivityService.updateToDelete(groupActivityId);

        return ResponseEntity.ok(true);
    }


    /**
     * 通过活动id获取下线信息
     */
    @GetMapping("/getOfflineHandleEventByActivityId/{activityId}")
    @PreAuthorize("@pms.hasPermission('group:activity:info')")
    public ResponseEntity<OfflineHandleEvent> getOfflineHandleEventByActivityId(@PathVariable("activityId") Long activityId) {
        OfflineHandleEvent offlineHandleEvent = offlineHandleEventService.getProcessingEventByHandleTypeAndHandleId(OfflineHandleEventType.GROUP_BUY.getValue(), activityId);
        return ResponseEntity.ok(offlineHandleEvent);
    }

    /**
     * 下线活动
     */
    @PostMapping("/offline")
    @PreAuthorize("@pms.hasPermission('group:activity:update')")
    public ResponseEntity<Void> offline(@RequestBody OfflineHandleEvent offlineHandleEvent) {
        GroupActivity groupActivity = groupActivityService.getById(offlineHandleEvent.getHandleId());
        if(groupActivity == null){
            // 未找到此活动，请稍后重试
            throw new YamiShopBindException("yami.activity.cannot.find");
        }
        Long sysUserId = SecurityUtils.getSysUser().getUserId();
        groupActivityService.offline(groupActivity, offlineHandleEvent.getOfflineReason(), sysUserId);
        return ResponseEntity.ok().build();
    }

    /**
     * 审核活动
     */
    @PostMapping("/auditGroupActivity")
    @PreAuthorize("@pms.hasPermission('group:activity:update')")
    public ResponseEntity<Void> auditGroupActivity(@RequestBody OfflineHandleEventAuditParam offlineHandleEventAuditParam) {
        Long sysUserId = SecurityUtils.getSysUser().getUserId();
        groupActivityService.auditGroupActivity(offlineHandleEventAuditParam, sysUserId);
        return ResponseEntity.ok().build();
    }

}
